/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.ejb;

import entities.Fantasyteam;
import entities.Week;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import tool.WeekConverter;

/**
 *
 * @author The Lunatics
 */
@Stateless
public class WeekFacade extends AbstractFacade<Week> {

    @PersistenceContext(unitName = "Touchdown308PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public WeekFacade() {
        super(Week.class);
    }

    public Week getWeek(String year, String week) {
        Integer id = new Integer(year + week);
        Query createNamedQuery = getEntityManager().createNamedQuery("Week.findById");
        createNamedQuery.setParameter("id", id);
        if (createNamedQuery.getResultList().size() > 0) {
            return (Week) createNamedQuery.getSingleResult();
        } else {
            return null;
        }
    }

    public Week getNextWeek(String year, String week) {
        Integer weekInt = new Integer(week);
        Integer yearInt = new Integer(year);
        if (weekInt > 17) {
            year = String.valueOf(yearInt + 1);
            week = "01";
        } else {
            week = String.valueOf(weekInt + 1);
        }
        return getWeek(year, WeekConverter.getWeekString(week));
    }
}
